import random
import base64
import smtplib
from email.mime.text import MIMEText

email_name = 'liu-f-h@qq.com' # 服务邮箱
passwd = 'qxlzkzwevoelcejj' # 授权码
# 你要把邮件发给谁
msg_to = "py123321abcd@163.com"
name = "慕慕手记"
fromname64 = base64.b64encode(bytes(name,"utf-8"))# 汉字转成base64
# b'xxxx'转成'xxxx'
fromname64str = str(fromname64,"utf-8")
# 尖括号拼接用双引号  (参考qq邮箱的规范：https://help.mail.qq.com/detail/0/994)
fromname = '"=?utf-8?B?{}=?="<{}>'.format(fromname64str,email_name)
# 正文
content = str(random.randint(10000,99999))
msg = MIMEText("您注册账号的验证码为:\n{}\n请不要告诉他人！".format(content))
msg["subject"] = "慕慕手记验证码"
msg["from"] = fromname
msg["to"] = msg_to
s = smtplib.SMTP_SSL("smtp.qq.com",465)  # 邮箱服务器及端口
s.login(email_name,passwd)
s.sendmail(email_name,msg_to,msg.as_string())